
/* Copyright 2023 Dual Tachyon
 * https://github.com/DualTachyon
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *     Unless required by applicable law or agreed to in writing, software
 *     distributed under the License is distributed on an "AS IS" BASIS,
 *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *     See the License for the specific language governing permissions and
 *     limitations under the License.
 */

#include <string.h>

#include "driver/eeprom.h"
#include "driver/st7565.h"
#include "driver/system.h"
#include "driver/backlight.h"
#include "external/printf/printf.h"
#include "helper/battery.h"
#include "settings.h"
#include "misc.h"
#include "ui/helper.h"
#include "ui/welcome.h"
#include "ui/status.h"
#include "audio.h"
#include "version.h"


void UI_DisplayReleaseKeys(void)
{
  memset(gStatusLine,  0, sizeof(gStatusLine));
  UI_DisplayClear();

  //UI_PrintString("RELEASE", 0, 127, 1, 10);
  //UI_PrintString("ALL KEYS", 0, 127, 3, 10);
  UI_PrintString("KONICHIWA", 0, 127, 1, 10);
  UI_PrintString("MOTHERFUCKER", 0, 127, 3, 10);

  ST7565_BlitStatusLine();  // blank status line
  ST7565_BlitFullScreen();
}

void UI_DisplayWelcome(void)
{
  char WelcomeString0[16];
  char WelcomeString1[16];

  memset(gStatusLine,  0, sizeof(gStatusLine));
  BACKLIGHT_TurnOn();
  UI_DisplayClear();

  if (gEeprom.POWER_ON_DISPLAY_MODE == POWER_ON_DISPLAY_MODE_FULL_SCREEN)
    {
#ifdef ENABLE_SPLASH
#include "utils/logo.h"
      for (int m = 4; m < LCD_WIDTH; m += 1)
	{
#ifdef UNCOMPRESSED_SPLASH
	  for (int yy = 0; yy < 7; yy += 1)
	    {
	      memset (gStatusLine, 0, m);
	      memcpy (gStatusLine+LCD_WIDTH-m, logo_bitmap, m);

	      uint8_t* p = (void*)gFrameBuffer + (yy * LCD_WIDTH);
	      memset (p, 0, m);
	      memcpy (p+LCD_WIDTH-m, logo_bitmap+((yy+1)*LCD_WIDTH), m);
	    }

	  ST7565_BlitStatusLine();
	  ST7565_BlitFullScreen();
	  SYSTEM_DelayMs(25);
	}
#endif
    }
  SYSTEM_DelayMs(1500);
#endif
#ifdef ENABLE_CW
  //welcome_morse();
#endif
  if (gF_LOCK)
    AUDIO_meep();

#if 0
#ifdef ENABLE_SPLASH
#include "utils/logo.h"
      for (int m = 4; m < LCD_WIDTH; m += 1)
	{
#ifdef UNCOMPRESSED_SPLASH
	  for (int yy = 0; yy < 7; yy += 1)
	    {
	      memcpy (gStatusLine, logo_bitmap, m);
	      memset (gStatusLine+m, 0, LCD_WIDTH-m);

	      uint8_t* p = (void*)gFrameBuffer + (yy * LCD_WIDTH);
	      memcpy (p, logo_bitmap+((yy+1)*LCD_WIDTH), m);
	      memset (p+m, 0, LCD_WIDTH-m);
	    }

	  ST7565_BlitStatusLine();
	  ST7565_BlitFullScreen();
	  SYSTEM_DelayMs(25);
	}
#endif
    }
  SYSTEM_DelayMs(1000);
#endif
#ifdef ENABLE_CW
  //welcome_morse();
#endif
#endif

  if (gEeprom.POWER_ON_DISPLAY_MODE == POWER_ON_DISPLAY_MODE_NONE)
    {
      for (int i = 0; i < 256; i++)
	ST7565_FillScreen((unsigned char)i);
    }

  if ((gEeprom.POWER_ON_DISPLAY_MODE != POWER_ON_DISPLAY_MODE_NONE) && (gEeprom.POWER_ON_DISPLAY_MODE != POWER_ON_DISPLAY_MODE_FULL_SCREEN))
    {
      memset(WelcomeString0, 0, sizeof(WelcomeString0));
      memset(WelcomeString1, 0, sizeof(WelcomeString1));

      if (gEeprom.POWER_ON_DISPLAY_MODE == POWER_ON_DISPLAY_MODE_VOLTAGE)
	{
	  strcpy(WelcomeString0, "VOLTAGE");
	  sprintf(WelcomeString1, "%u.%02uV %u%%",
		  gBatteryVoltageAverage / 100,
		  gBatteryVoltageAverage % 100,
		  BATTERY_VoltsToPercent(gBatteryVoltageAverage));
	}
      else
	{
	  EEPROM_ReadBuffer(0x0EB0, WelcomeString0, 16);
	  EEPROM_ReadBuffer(0x0EC0, WelcomeString1, 16);
	}

      UI_PrintString(WelcomeString0, 0, 127, 0, 10);
      UI_PrintString(WelcomeString1, 0, 127, 2, 10);
      UI_PrintStringSmallBold(Version, 0, 127, 4);
      UI_PrintStringSmallNormal(__DATE__, 0, 127, 6);

      ST7565_BlitStatusLine();  // blank status line
      ST7565_BlitFullScreen();
    }
}
